const Koa = require('koa');
const cheerio = require('cheerio');
const https = require('https');
const fs = require('fs');
const app = new Koa();

let content = '';
for (let index = 0; index < 9; index++) {
  httpRequest(index, content);
}
function httpRequest(currPageNo, content) {
  let singleContent = '';
  const url = currPageNo === 0 ? 'https://www.leixuesong.cn/category/web' : `https://www.leixuesong.cn/category/web/page/${currPageNo + 1}`;
  https.get(url, (res) => {
    res.on('data', (data) => {
      singleContent += data;
    });
    res.on('end', () =>{
      getContent(singleContent, currPageNo);
  
    })
  }).on('error', () => {
    console.log('error');
  })
}

app.use(async(ctx) => {
  ctx.body = content;
})

app.listen(3030, () => {
  console.log('start');
})

function getContent(singleContent, currPageNo) {
  const $ = cheerio.load(singleContent);
  const eventItems = $('.event-items .span12');
  let str = '';
  eventItems.each((i, item) => {
    const aTag = $(item).find('h3 a');
    const title = aTag.attr('title');
    const aHref = aTag.attr('href');
    str += `第${currPageNo + 1}页：${title}-------${aHref}\n`;
  })
  content += str;
  if (currPageNo === 8) {
    writeFile(content);
  }
}

function writeFile(str) {
  fs.writeFile('./data.txt', str, (err) => {
    if (err) {
      console.log(err);
    }
  })
}